package com.example.singleactivitytemplate.ui.base

import android.annotation.SuppressLint
import android.app.Dialog
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.*
import android.widget.TextView
import androidx.annotation.FloatRange
import androidx.annotation.Nullable
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.example.singleactivitytemplate.R


/**
 * DialogFragment 基础类
 *
 * @author joymed
 */
abstract class BaseDialogFragment<VB : ViewBinding, VM : BaseViewModel> : DialogFragment() {
    protected open lateinit var viewBinding: VB
    protected open lateinit var mViewModel: VM



    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {


        setStyle(STYLE_NO_TITLE, R.style.DialogStyle)
        isCancelable = false

        viewBinding = getViewBindingClass(inflater, container)

        mViewModel = ViewModelProvider(this)[getViewModelClass()]



        return viewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        try {

            initOnListener()
            initObserver()
            initData()
        } catch (e: Exception) {

        }
    }

    override fun onCreateDialog(@Nullable savedInstanceState: Bundle?): Dialog {
        return object : Dialog(requireActivity(), theme) {
            override fun dispatchTouchEvent(motionEvent: MotionEvent): Boolean {

                return super.dispatchTouchEvent(motionEvent)
            }
        }
    }



    fun show(fragmentActivity: FragmentActivity) {
        show(fragmentActivity.supportFragmentManager)
    }

    fun show(fragment: Fragment) {
        show(fragment.childFragmentManager)
    }

    private fun show(manager: FragmentManager) {
        try {
            manager.beginTransaction().remove(this).commitAllowingStateLoss()
            super.show(manager, javaClass.simpleName)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onDetach() {
        super.onDetach()

        detach()
    }

    /**
     * 显示toast
     */
    fun toast(msg: String?) {

    }

    fun showInfo(msg: String?) {

    }

    private fun showInfo(type: Int, msg: String?) {

    }

    /**
     * 获取view布局Binding
     */
    abstract fun getViewBindingClass(inflater: LayoutInflater, container: ViewGroup?): VB
    protected abstract fun getViewModelClass(): Class<VM>

    /**
     * 初始化视图和数据
     */
    protected abstract fun initData()

    protected abstract fun initOnListener()

    protected abstract fun initObserver()

    /**
     * @return dialog的宽度比例 0=内容包裹，1=匹配父布局， 0.0~1.0 按屏幕比例计算
     */
    @get:FloatRange(from = 0.0, to = 1.0)
    abstract val widthPercentSize: Float

    /**
     * @return dialog的高度比例 0=内容包裹， 1=匹配父布局， 0.0~1.0 按屏幕比例计算
     */
    @get:FloatRange(from = 0.0, to = 1.0)
    abstract val heightPercentSize: Float

    /**
     * @return dialog相对于左上角的x,y
     */
    open fun position(): IntArray {
        return intArrayOf(0, 0)
    }

    /**
     * @return dialog非显示区域透明度
     */
    open fun dimAmount(): Float {
        return 0.6f
    }

    /**
     * 销毁
     */
    protected abstract fun detach()
}